﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell;

namespace Microsoft.WowAddonStudio.Project.OptionPages
{
    [Guid(GuidStrings.FrameXmlDesignerOptionsDialogPage)]
    public class FrameXmlDesignerGeneralOptionsDialogPage : DialogPage
    {
        private readonly FrameXmlDesignerGeneralOptionsControl control = new FrameXmlDesignerGeneralOptionsControl();

        public event EventHandler BackgroundImageFileChanged
        {
            add { control.BackgroundImageFileChanged += value; }
            remove { control.BackgroundImageFileChanged -= value; }
        }

        /// <summary>
        /// </summary>
        /// <value></value>
        /// <!-- Failed to insert some or all of included XML -->
        /// <include file="doc\DialogPage.uex" path="docs/doc[@for=&quot;DialogPage&quot;.Window]"/>
        /// <devdoc>
        /// The window this dialog page will use for its UI.
        /// This window handle must be constant, so if you are
        /// returning a Windows Forms control you must make sure
        /// it does not recreate its handle.  If the window object
        /// implements IComponent it will be sited by the
        /// dialog page so it can get access to global services.
        /// </devdoc>
        protected override IWin32Window Window
        {
            get { return control; }
        }

        /// <summary>
        /// Gets or sets the background image file.
        /// </summary>
        /// <value>The background image file.</value>
        public string BackgroundImageFile
        {
            get { return control.BackgroundImageFile; }
            set { control.BackgroundImageFile = value; }
        }

        /// <summary>
        /// </summary>
        /// <param name="e"></param>
        /// <!-- Failed to insert some or all of included XML -->
        /// <include file="doc\DialogPage.uex" path="docs/doc[@for=&quot;DialogPage.OnApply&quot;]"/>
        /// <devdoc>
        /// This method is called when VS wants to save the user's
        /// changes then the dialog is dismissed.
        /// </devdoc>
        protected override void OnApply(PageApplyEventArgs e)
        {
            if (!String.IsNullOrEmpty(BackgroundImageFile) && !File.Exists(BackgroundImageFile))
            {
                e.ApplyBehavior = ApplyKind.Cancel;
                MessageBox.Show(Window, "The specified path for the background image file is invalid. Please enter a valid path.",
                                "AddOn Studio for World of Warcraft", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
                base.OnApply(e);
        }
    }
}